// https://www.lintcode.com/problem/meeting-rooms/my-submissions

/**
 * Definition of Interval:
 * classs Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this->start = start;
 *         this->end = end;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param intervals: an array of meeting time intervals
     * @return: if a person could attend all meetings
     */
     
//     void sort(vector<Interval> &intervals)
//     {
//         for (int i = intervals.size() - 1; i >= 0; --i)
//         {
//             for (int j = i; j >= 0; --j)
//             {
//                 if (intervals[i].start < intervals[j].start)
//                 {
//                     Interval tmp = intervals[i];
//                     intervals[i] = intervals[j];
//                     intervals[j] = tmp;
//                 }
//             }
//         }
//     }
//     bool canAttendMeetings(vector<Interval> &intervals) {
// // Input
// // []
// // Expected
// // true
//         int end = INT_MIN;
//         sort(intervals);
//         for (int i = 0; i < intervals.size(); ++i)
//         {
//             if (intervals[i].start < end)
//             {
//                 return false;
//             }
//             end = max(end, intervals[i].end);
//         }
//         return true;
//     }
    
    
    // 法二：通用做法
    bool canAttendMeetings(vector<Interval> &intervals) {
        map<int, int> record;
        for (int i = 0; i < intervals.size(); ++i)
        {
            record[intervals[i].start]++;
            record[intervals[i].end]--;
        }
        int max_val = 0;
        int sum = 0;
        // for (map<int, int>::iterator it = record.begin(); it != record.end(); ++it)
        // {
        // max_val = max(max_val, sum += it->second);    
        for (auto& i: rec) {
            sum += i.second;  //注意iterator用->second
            if (sum > 1) return false;
        }
        return true;
    }
};